package com.lanyou.esb.cook.proxy.redis;

import com.lanyou.esb.cook.proxy.common.ParamFactory;
import com.lanyou.esb.cook.proxy.service.FieldSystemService;
import com.lanyou.esb.cook.proxy.service.HttpService;
import com.lanyou.esb.cook.proxy.service.IfReqQuantityService;
import com.lanyou.esb.cook.proxy.service.ParamSetService;
import com.lanyou.esb.cook.proxy.service.TokenService;

/**
 * 启动加载数据
 * 
 * @author Davey.wu
 */
public class RedisListeners {
	/** 参数配置 **/
	private ParamSetService paramSetService;
	/** 领域系统服务 **/
	private FieldSystemService fieldSystemService;
	/** Http接口服务 **/
	private HttpService httpService;
	/** 接口令牌服务 **/
	private TokenService tokenService;
	/** 接口请求量控制服务 **/
	private IfReqQuantityService ifReqQuantityService;

	/**
	 * 初始化资源
	 */
	public void init() {
		// try {
		// InitialContext namingContext = new InitialContext();
		// BasicDataSource dataSource = (BasicDataSource) namingContext
		// .lookup("osgi:services/org.apache.commons.dbcp.BasicDataSource/(osgi.jndi.service.name=jdbc/emconnector)");
		// System.out.println(dataSource);
		// } catch (NamingException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

		System.out.println("+++++加载基础数据  start++++++++");
		// 加载系统参数
		paramSetService.init();
		// 加载领域系统，接口，令牌等数据
		load();
		System.out.println("+++++加载基础数据  end  ++++++++");
	}

	/**
	 * 销毁资源
	 */
	public void destroy() {
		// 关闭并销毁REDIS连接
		JedisPoolUtils.destroy();
		// 清除配置参数
		ParamFactory.clear();
	}

	/**
	 * 加载基础数据
	 */
	public void load() {
		try {
			// 加载系统
			fieldSystemService.loadAllToRedis();
			// 加载接口
			httpService.loadAllToRedis();
			// 加载令牌
			tokenService.loadAllToRedis();
			// 加载流量控制
			ifReqQuantityService.loadAllToRedis();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public FieldSystemService getFieldSystemService() {
		return fieldSystemService;
	}

	public void setFieldSystemService(FieldSystemService fieldSystemService) {
		this.fieldSystemService = fieldSystemService;
	}

	public HttpService getHttpService() {
		return httpService;
	}

	public void setHttpService(HttpService httpService) {
		this.httpService = httpService;
	}

	public TokenService getTokenService() {
		return tokenService;
	}

	public void setTokenService(TokenService tokenService) {
		this.tokenService = tokenService;
	}

	public IfReqQuantityService getIfReqQuantityService() {
		return ifReqQuantityService;
	}

	public void setIfReqQuantityService(
			IfReqQuantityService ifReqQuantityService) {
		this.ifReqQuantityService = ifReqQuantityService;
	}

	public ParamSetService getParamSetService() {
		return paramSetService;
	}

	public void setParamSetService(ParamSetService paramSetService) {
		this.paramSetService = paramSetService;
	}

}
